// pages/p6-save-share/save-share.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    root: app.globalData.root,
  },
  getlocationbtn: function () { 
    console.log('getlocationbtn');
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success (res) {
        const latitude = res.latitude
        const longitude = res.longitude
        wx.openLocation({
          latitude,
          longitude,
          scale: 18
        })
      }
     })

  },
  //将canvas转换为图片保存到本地，然后将图片路径传给image图片的src
  saveImage: function () {
    wx.showLoading({
      title: "保存图片中...",
    });
    var that = this;
    var context = wx.createCanvasContext("mycanvas");
    var path = '../../images/bgimg.jpg';
    var urlnet = 'https://dev.benbun.com/web/baidu/chengshimingpian/img/share.jpg';
    var urlnet_lock = '../../../images/xiala-img.png';
    console.log(urlnet_lock)
    
    //这个地方的图片是需要注意，图片需要下载不然，手机上不能正常显示
    // downLoadBgImg(urlnet);//网络图片处理
    downLoadBgImg(urlnet_lock);//本地图片处理
    // 保存到本地
    function drawAgain() {
      context.draw(true, setTimeout(function () {
        wx.canvasToTempFilePath({
          x: 0,
          y: 0,
          width: 750,
          height: 1250,
          destWidth: 750,
          destHeight: 1250,
          canvasId: "mycanvas",
          success: function (res) {
            that.setData({
              imagePath: res.tempFilePath
            });
            //图片保存到手机
            wx.saveImageToPhotosAlbum({
              filePath: that.data.imagePath,
              success(res) {
                wx.hideLoading();
                wx.showModal({
                  content: "图片已保存到相册",
                  showCancel: false,
                  confirmText: "好的",
                  confirmColor: "#333",
                  success: function (res) {
                    if (res.confirm) {
                      // console.log("用户点击确定");
                      /* 该隐藏的隐藏 */
                      // console.log(11111);
                    }
                  },
                  fail: function (res) {
                    // console.log(22222);
                  }
                });
              },
              fail: function (res) {
                wx.hideLoading();
                if (res.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                  wx.showToast({
                    title: '请打开授权',
                    icon: 'loading',
                    duration: 1000
                  });
                  that.setData({
                    showFlag: true
                  });
                }
              }
            });
          }
        });
      }, 2000));
    }
    function downLoadBgImg(netUrl) {
      wx.getImageInfo({
        src: netUrl,
        success: function (res) {
          var canvasImg = '';
          wx.setStorageSync(canvasImg, res.path);
          canvasImg = wx.getStorageSync(canvasImg);
          context.drawImage(canvasImg, 0, 0, 686, 686);
          context.drawImage(path, 140, 140, 100, 100);
          drawAgain();
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})